<template>
    <el-form ref="formRef" :model="formData" label-width="86px" :disabled="pageType == 'look'">
        <el-form-item class="welcome_type" label="欢迎语设置">
            <el-radio-group v-model="formData.welcome.type">
                <el-radio :label="1">新建欢迎语</el-radio>
                <el-radio :label="3">已有欢迎语</el-radio>
            </el-radio-group>
            <div v-if="formData.welcome.type == 3" style="margin-bottom: 10px">
                <el-button @click="onChooseWelcome">选择已有欢迎语</el-button>
            </div>
        </el-form-item>
        <el-form-item prop="welcome_info.welcome_text" :rules="rules.content">
            <div class="content_detail">
                <div class="content_left">
                    <div class="list_item mt20">
                        <MaterialContent
                            ref="materialContentRef"
                            :max-num="8"
                            :is-hidden-label="formData.welcome_type == 3"
                            :input-content-tabs="inputContentTabs"
                            :show-material-tabs="showMaterialTabs"
                            :input-content-data="inputContentData"
                            :material-content-data="materialContentData"
                            :welcome-friend-id="welcomeFriendId"
                            :placeholder="'请输入欢迎语'"
                            :disabled="pageType == 'look'"
                            @change="changeMaterial" />
                    </div>
                </div>
                <div class="content-right">
                    <div class="title">欢迎语预览</div>
                    <PhonePreview ref="phonePreviewRef" />
                </div>
            </div>
        </el-form-item>
        <div style="text-align: center; border-top: 1px solid #d9d9d9; margin-bottom: 20px"></div>
        <share-link-settings ref="shareLinkSettingsRef" v-model="formData" class="share-link-setting-form" />
    </el-form>
    <!-- <ZWMatericalLib ref="ZWMatericalLibRef" @submit="getFiles" /> -->
    <ZWDepart ref="DepartRef" :type="1" @submit="getDepart" />
    <BatchLabel ref="BatchLabelRef" @save="getTags" />
    <SelectAlreadyMaterial
        ref="selectAlreadyMaterialRef"
        v-model="selectAlreadyMaterialShow"
        @select="selectAlreadyMaterialFn" />
    <!-- <ChooseWelcome ref="ChooseWelcomeRef" @submit="submitHandel" /> -->
</template>

<script setup lang="ts">
import MaterialContent from '@/components/zw/materialContent/index.vue';
import PhonePreview from '@/components/zw/phonePreview/welcomePhonePreview/materialPreview/index.vue';
import SelectAlreadyMaterial from '@/components/zw/selectAlreadyMaterial/index.vue';
import ShareLinkSettings from '@/components/zw/activity/share-link-settings/index.vue';

// import Phone from '@/components/welcome/Phone.vue'
// import ChooseWelcome from '@/components/welcome/ChooseWelcome.vue'
import UploadImg from '@/components/welcome/UploadImg.vue';
import ZWDepart from '@/components/ZWDepart.vue';
import BatchLabel from '@/components/BatchLabel.vue';
// import ZWMatericalLib from '@/components/channelNew/ZWMatericalLibNew.vue'
// import ZWMatericalLib from '@/components/channelNew/ZWMatericalLib.vue'
// import ZWEmoji from '@/components/ZWEmoji.vue'
import { ref, reactive, withDefaults, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { getSetionId, useInsertWelcomeContent, initMaterialData, initAlreadyMaterialData } from '@/utils/utils';
const materialContentRef = ref<HTMLElement | null>();
const phonePreviewRef = ref<HTMLElement | null>();
const inputContentTabs = ref<Array<number>>([1, 2, 3, 4]); // 配置欢迎语输入框选项
const showMaterialTabs = ref<Array<string>>(['1', '2', '3', '4', '5', '6', '7', '9', '11', '10']); // 配置选择素材组件类型
const inputContentData = ref<string>(''); //  用户存放回显数据  欢迎语输入框
const materialContentData = ref<Array<any>>([]);
const selectAlreadyMaterialRef = ref<HTMLElement | null>();
const selectAlreadyMaterialShow = ref<boolean>(false);
interface Props {
    pageType: any;
}
const props = withDefaults(defineProps<Props>(), {
    pageType: ''
});
const emit = defineEmits(['submit']);
const uploadList = reactive([
    { id: '2', icon: '/static/file/pic.png', name: '图片' },
    { id: '3', icon: '/static/file/mp3.png', name: '音频' },
    { id: '4', icon: '/static/file/mov.png', name: '视频' },
    { id: '5', icon: '/static/file/ysb.png', name: '文件' },
    { id: '6', icon: '/static/file/link.png', name: '链接' },
    { id: '8', icon: '/static/file/wbgg.png', name: '抽奖' },
    { id: '27', icon: '/static/file/form.png', name: '收集表' },
    { id: '99', icon: '/static/file/shop.png', name: '电商' },
    { id: '15', icon: '/static/file/official.png', name: '公众号' },
    { id: '11', icon: '/static/file/miniApp.png', name: '小程序' }
]);
const onURL = (data: any) => {
    const fileTYpe = {
        '.pdf': '/static/file/pdf.png',
        '.doc': '/static/file/doc.png',
        '.docx': '/static/file/doc.png',
        '.xls': '/static/file/xls.png',
        '.xlsx': '/static/file/xls.png',
        '.ppt': '/static/file/ppt.png',
        '.pptx': '/static/file/ppt.png'
    };
    if (data) {
        const last = data.substring(data.lastIndexOf('.'));
        return fileTYpe[last] || '/static/file/file.png';
    }
};
const formRef = ref<any>(null);
const DepartRef: any = ref<any>(null); // 部门弹框类型
const ZWMatericalLibRef: any = ref(null);
const BatchLabelRef: any = ref<any>(null); // 选择标签弹框
const emojiVisible = ref(false);
const fileList = ref([]);
const creators = ref([]); // 创建人options
const AllTags = ref([]); // 选择标签options
const welcome_flag = ref(1);
const formData = ref<any>({
    welcome: {
        type: 1,
        content: 'eeeee',
        attachment: [],
        local_link: [],
        local_content: []
    },
    welcome_info: {},
    time_welcome_flag: 2,
    time_welcome: [{ content: 'eeeee1', week: [] }],
    /** 奖品图片 */
    link_img: 'https://static.scrm.keyid.cn/assets/images/web-pc/default-fission-share-avatar.png', // 是url
    /** 链接标题 */
    link_title: '呼朋唤友, 免费拿奖品',
    /** 链接描述 */
    link_desc: '好友，你可以给我打个call吗？',
    tag_list: []
});
const welcomeFriendId = ref<any>('');

const onInsertText = useInsertWelcomeContent('welcomeContent', formData.value, 'welcome.content');
const rules = reactive({
    user_list: [{ required: true, message: '请选择引流员工', trigger: 'change' }],
    link_img: [{ required: true, message: '请选择图文封面', trigger: 'change' }],
    link_title: [{ required: true, message: '请输入活动标题', trigger: 'blur' }],
    link_desc: [{ required: true, message: '请输入活动描述', trigger: 'blur' }],
    content: [{ required: true, message: '请输入欢迎语内容', trigger: 'blur' }],
    week: [{ required: true, message: '请选择发送周期', trigger: 'change' }],
    send_time: [{ required: true, message: '请选择发送时间', trigger: 'change' }]
});
const onDelTime = (index: any) => {
    formData.value.time_welcome.splice(index, 1);
    weekOptionList.value.splice(index, 1);
};
const weekOptionList = ref<any>([]);
const onIsTimeDisabled = (data: any, active: any) => {
    // 星期
    let weekOption = [
        { label: 1, name: '周一', disabled: false },
        { label: 2, name: '周二', disabled: false },
        { label: 3, name: '周三', disabled: false },
        { label: 4, name: '周四', disabled: false },
        { label: 5, name: '周五', disabled: false },
        { label: 6, name: '周六', disabled: false },
        { label: 0, name: '周日', disabled: false }
    ];
    const clearWeek: any = [];
    let weekList: any = [];
    formData.value.time_welcome.forEach((item: any, index: any) => {
        if (data.send_time[0] == item.send_time[0] && data.send_time[1] == item.send_time[1] && index != active) {
            if (index != active) {
                weekList = [...weekList, ...item.week];
            }
            weekOption.forEach((it: any, i: any) => {
                weekOption[i].disabled = false;
                if (item.week.includes(it.label)) {
                    weekOption[i].disabled = true;
                } else {
                    clearWeek.push(it.label);
                }
            });
        }
    });
    let num = 0;

    formData.value.time_welcome.forEach((item: any, i: any) => {
        if (data.send_time[0] == item.send_time[0] && data.send_time[1] == item.send_time[1]) {
            num++;
        }
    });
    if (num == 1) {
        weekOption = [
            { label: 1, name: '周一', disabled: weekList.includes(1) },
            { label: 2, name: '周二', disabled: weekList.includes(2) },
            { label: 3, name: '周三', disabled: weekList.includes(3) },
            { label: 4, name: '周四', disabled: weekList.includes(4) },
            { label: 5, name: '周五', disabled: weekList.includes(5) },
            { label: 6, name: '周六', disabled: weekList.includes(6) },
            { label: 0, name: '周日', disabled: weekList.includes(0) }
        ];
    }

    weekOptionList.value[active] = weekOption;
};
const onData = (index: any, data: any) => {
    formData.value.time_welcome[index] = data;
    formData.value.time_welcome.forEach((item: any, i: any) => {
        onIsTimeDisabled(item, i);
    });
};
//打开新建选择人
const openDepart = () => {
    DepartRef.value.departVisble = true;
    DepartRef.value.selectedMember = [...creators.value];
};
// 获取部门成员
const getDepart = (res: any) => {
    creators.value = res;
    formData.value.user_list = getSetionId('id', creators.value);
};
// 员工数据
const tags = reactive([]);
// 员工移除
const handleClose = (tag: any) => {
    creators.value = creators.value.filter((item: any) => item.id !== tag.id);
    formData.value.user_list = getSetionId('id', creators.value);
    DepartRef.value.removeItem(tag);
};
// 打开选择标签
const selecTag = () => {
    BatchLabelRef.value.hasConditiion = false;
    BatchLabelRef.value.dialogData.labelChooseType = 2;
    BatchLabelRef.value.chooseDataArr = [...AllTags.value];
    BatchLabelRef.value.dialogVisible = true;
};
// 获取标签
const getTags = (data: any) => {
    AllTags.value = data.data;
    formData.value.tag_list = getSetionId('id', data.data);
};
const previewData = ref<any>([]);

const selectAlreadyMaterialFn = (data: any) => {
    const { content, welcome_friend_attachment, id } = data;
    welcomeFriendId.value = id;
    inputContentData.value = content;
    materialContentData.value = initAlreadyMaterialData(welcome_friend_attachment);
};

const onChooseWelcome = () => {
    selectAlreadyMaterialShow.value = true;
};
const onLookData = (val: any) => {
    welcomeFriendId.value = val.welcome_info?.welcome_friend_id;
    inputContentData.value = val.welcome_info.welcome_text;
    materialContentData.value = initMaterialData(val.welcome_info.welcome_material);
    // setShareSettingForm(val);
};
const lookData = ref<any>([]);
const onLookTime = (v: any) => {
    formData.value.time_welcome = v;
    lookData.value = v;
};
const onSubmit = async () => {
    if (!formRef.value) return;
    await formRef.value!.validate(async (valid: any, fields: any) => {
        // 校验自定义分享内容
        const result = await getShareSettingForm();
        if (result === false) return;

        if (valid) {
            emit('submit');
        } else {
            const num = +Object.keys(fields)[0][13] + 1;
            const message = num
                ? `时段${num}：${Object.values(fields)[0][0].message}`
                : Object.values(fields)[0][0].message;
            ElMessage.warning(message);
        }
    });
};
const changeMaterial = (val: any) => {
    formData.value.welcome_info = val.welcome_info;
};

/* ----------------------------自定义分享内容-start---------------------------- */
/** 自定义分享内容链接 */
const shareLinkSettingsRef = ref();

/**
 * 获取分享设置内容
 */
const getShareSettingForm = async () => {
    return await shareLinkSettingsRef.value.getFormParams();
};

/**
 * 设置分享设置表单内容
 */
const setShareSettingForm = (data: any) => {
    shareLinkSettingsRef.value.setFormParams(data);
};
/* ----------------------------自定义分享内容-end---------------------------- */

defineExpose({
    formData,
    AllTags,
    creators,
    previewData,
    welcome_flag,
    getDepart,
    onSubmit,
    onLookData,
    onLookTime
});
</script>

<style lang="scss" scoped>
.content_detail {
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    .content_left {
        flex: 1;
        padding-right: 48px;
        border-right: 1px solid #eee;
        .list_item {
            padding-left: 1.125rem;
            padding-bottom: 24px;
            border-bottom: 0.0625rem solid #eaeaea;
        }
        .mt20 {
            margin-top: 1.25rem;
        }
    }
    .content-right {
        padding: 0 24px 0 48px;
        .title {
            text-align: center;
            line-height: 3.5;
            font-size: 14px;
            color: #333;
        }
    }
}
.welcome_type {
    margin-bottom: 0;
}
.img_box {
    display: grid;
    grid-template-columns: 64px 110px 1fr;
    grid-gap: 20px;

    .text {
        margin-left: 15px;
        padding-top: 80px;
        font-size: 12px;
        color: #999;
    }
}
$num: 2;
@mixin ellips($num) {
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: $num;
    line-clamp: $num;
    -webkit-box-orient: vertical;
}
:deep .el-checkbox {
    margin-right: 18px;
}
.pop_select_box {
    height: 84px;

    .upload_btn {
        margin-top: 8px;
    }

    .upload_pop {
        background: #ffffff;
        border-radius: 12px;
        // box-shadow: 0px 2px 4px 0px rgba(216, 216, 216, 0.5);
        display: flex;
        align-items: center;
        justify-content: space-around;

        .item_pop {
            width: 80px;
            height: 62px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;

            .item_pop_icon {
                width: 40px;
                cursor: pointer;
                border: 1px solid transparent;
                &:hover {
                    border: 1px solid #ed742e;
                }
            }

            .item_pop_name {
                white-space: nowrap;
                font-size: 14px;
                margin-top: 2px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #1a1918;
            }
        }
    }
}
.container {
    position: relative;
    width: 600px;
    height: 480px;
    background: #f8f8f8;
    border-radius: 4px;
    :deep(.el-textarea__inner) {
        border: none;
        box-shadow: none;
        background-color: #f8f8f8;
        border-bottom: 1px solid #eaeaea;
    }
    .text_btn {
        display: flex;
        justify-content: space-between;
        padding: 12px;
        color: #ff6b00;
    }
    .text_hover {
        position: absolute;
        bottom: 15px;
        left: 15px;
        z-index: 10;
        display: flex;
        align-items: center;
        cursor: pointer;
        padding: 0 12px;
        color: #969696;
        background: #f8f8f8;
        border: 1px solid #d9d9d9;
    }
    .upload_list_box {
        overflow: hidden;
        height: 230px;
    }

    .upload_list {
        .upload_item {
            margin-top: 5px;
            width: 440px;
            height: 32px;
            box-sizing: border-box;
            background: #eaeaea;
            display: flex;
            padding: 0px 10px 0px 14px;
            align-items: center;

            .file_icon {
                width: 20px;
                height: 20px;
                margin-right: 7px;
            }

            .file_name {
                width: 375px;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
            }

            .del_icon {
                width: 10px;
                height: 10px;
                cursor: pointer;
            }
        }
    }
    .prev_box {
        flex: 1;
        position: absolute;
        right: -480px;
        top: 0;

        .phone_con {
            // justify-content: center;
            position: relative;
            margin: -20px auto;
            width: 244px;
            height: 495px;
            box-sizing: border-box;
            border-radius: 36px;
            border: 9px solid #eef5fe;
            background: url('@/assets/image/channel/phone_bg.png');
            background-size: cover;

            .phone-title {
                position: absolute;
                top: -40px;
                left: 75px;
            }

            .phone_List {
                width: 100%;
                box-sizing: border-box;
                position: absolute;
                top: 48px;
                height: 380px;
                bottom: 48px;
                padding: 7px 10px;

                .bg_friend {
                    // width: 100%;

                    .public_title {
                        font-size: 11px;
                        margin-top: -10px;
                        text-align: center;
                        font-family: STHeitiSC-Light, STHeitiSC;
                        font-weight: 300;
                        color: #33302d;
                    }
                    .public_time {
                        margin-top: -19px;
                        text-align: center;
                        font-size: 11px;
                        font-family: STHeitiSC-Light, STHeitiSC;
                        font-weight: 300;
                        color: #969696;
                    }
                    .user_con {
                        margin-top: 10px;
                        display: flex;
                        .user_avator {
                            width: 22px;
                            height: 22px;
                            margin-right: 4px;
                        }
                        .friend_main {
                            flex: 1;
                            .user_name {
                                font-size: 12px;
                                margin-top: -8px;
                                font-family: STHeitiSC-Medium, STHeitiSC;
                                font-weight: 500;
                                color: #969696;
                                transform: scale(0.6);
                                transform-origin: left top;
                                margin-bottom: 4px;
                            }
                            .welcomeWords {
                                position: relative;
                                margin-top: -23px;
                                font-size: 12px;
                                color: #969696;
                                line-height: 15px;
                                transform: scale(0.6);
                                transform-origin: left top;
                                width: 160px;
                                box-sizing: border-box;
                                height: 67px;
                                overflow: hidden;
                                padding: 5px 10px;
                                background-color: #fff;
                                border-radius: 5px;
                                @include ellips(4);
                                &::after {
                                    content: '';
                                    position: absolute;
                                    left: -5px;
                                    top: 10px;
                                    width: 0;
                                    height: 0;
                                    border-top: 5px solid transparent;
                                    border-right: 8px solid #fff;
                                    border-bottom: 5px solid transparent;
                                }
                            }
                            .send_image {
                                width: 68px;
                                height: 46px;
                                margin-top: -23px;
                            }
                            .video_list {
                                position: relative;
                                width: 80px;
                                height: 55px;
                                margin-top: -23px;

                                .video_image {
                                    width: 100%;
                                    height: 100%;
                                }
                                .video_play {
                                    width: 15px;
                                    height: 15px;
                                    position: absolute;
                                    left: 50%;
                                    top: 50%;
                                    transform: translate(-50%, -50%);
                                }
                            }
                            .user_link {
                                // margin-left: 5px;
                                margin-top: -23px;
                                position: relative;
                                width: 151px;
                                height: 65px;
                                box-sizing: border-box;
                                padding: 5px 10px;
                                border-radius: 5px;
                                background: #ffffff;
                                &::after {
                                    content: '';
                                    position: absolute;
                                    left: -5px;
                                    top: 10px;
                                    width: 0;
                                    height: 0;
                                    border-top: 5px solid transparent;
                                    border-right: 8px solid #fff;
                                    border-bottom: 5px solid transparent;
                                }
                                .user_link_tit {
                                    width: 100%;
                                    font-size: 12px;
                                    font-family: STHeitiSC-Light, STHeitiSC;
                                    font-weight: 300;
                                    color: #000000;
                                    transform: scale(0.83);
                                    transform-origin: left top;
                                    @include ellips(2);
                                }
                                .user_link_main {
                                    margin-top: 4px;
                                    display: flex;
                                    .user_link_desc {
                                        width: 110px;
                                        font-size: 12px;
                                        font-family: STHeitiSC-Light, STHeitiSC;
                                        font-weight: 300;
                                        color: #969696;
                                        transform: scale(0.6);
                                        transform-origin: left top;
                                        margin-top: 2px;
                                        @include ellips(3);
                                    }
                                    .user_link_icon {
                                        width: 22px;
                                        height: 22px;
                                    }
                                }
                            }
                            .user_file {
                                position: relative;
                                // margin-left: 5px;
                                margin-top: -23px;
                                width: 150px;
                                height: 48px;
                                box-sizing: border-box;
                                padding: 8px;
                                border-radius: 3px;
                                background: #ffffff;
                                &::after {
                                    content: '';
                                    position: absolute;
                                    left: -5px;
                                    top: 6px;
                                    width: 0;
                                    height: 0;
                                    border-top: 5px solid transparent;
                                    border-right: 10px solid #fff;
                                    border-bottom: 5px solid transparent;
                                }
                                .user_file_main {
                                    display: flex;
                                    .user_file_desc {
                                        width: 105px;
                                        font-size: 12px;
                                        transform: scale(0.83);
                                        transform-origin: left top;
                                        margin-bottom: 2px;
                                        font-family: STHeitiSC-Light, STHeitiSC;
                                        font-weight: 300;
                                        color: #000000;
                                        @include ellips(2);
                                    }
                                    .user_file_icon {
                                        width: 30px;
                                        height: 30px;
                                    }
                                }
                                .user_file_size {
                                    // margin-top: 2px;
                                    font-family: STHeitiSC-Light, STHeitiSC;
                                    font-weight: 300;
                                    color: #969696;
                                    font-size: 12px;
                                    transform: scale(0.6);
                                    transform-origin: left top;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    .welc_time_top {
        display: flex;
        align-items: center;
        width: 600px;
        height: 40px;

        font-size: 14px;
        font-family: STHeitiSC-Medium, STHeitiSC;
        font-weight: 500;
        color: #33302db3;
        line-height: 20px;
        background: #fffaf9;
        .radius_left {
            width: 8px;
            height: 8px;
            margin: 0 20px;
            border-radius: 50%;
            background: #ff6b00;
        }
    }
    .welc_content_t {
        display: flex;
        align-items: center;
        margin: 20px 0;
        .welc_color {
            width: 3px;
            height: 18px;
            background: #ff6b00;
            margin-right: 10px;
        }
    }
    .welc_content_c {
        width: 600px;
        height: 103px;
        padding: 0 0 0 10px;
        background: #ffffff;
        border: 1px solid #eaeaea;
        .check_box {
            display: flex;
            margin-bottom: 6px;
        }
    }
}
.form-staffs {
    display: flex;

    .title {
        margin-right: 12px;
    }

    .staffs {
        &_item {
            height: 22px;
            line-height: 22px;
            margin-right: 6px;
            // padding: 0;
            color: #35383a;
            border: 1px solid #fffaf9;
            border-radius: 1px 1px 1px 1px;
            background: #fffaf9;
        }

        :deep .el-tag .el-icon {
            width: 12px;
            height: 12px;
            color: #ffffff;
            background: #d9d9d9;
        }

        :deep .el-tag .el-icon svg {
            margin: 0px;
        }

        :deep .el-tag .el-tag__close:hover {
            background: #d9d9d9;
        }

        :deep span.el-tag.el-tag--light.staffs_item:before {
            content: '';
            display: inline-block;
            width: 10px;
            height: 11px;
            background: url('@/assets/image/LiveCode/people_icon.png') center/100% no-repeat;
            margin-right: 10px;
        }
    }
}
.alias {
    margin-left: 15px;
}
.share-link-setting-form {
    width: 720px;
}
</style>
